package org.example.studentsystem_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.studentsystem_back.common.CommonResponse;
import org.example.studentsystem_back.domain.Money;
import org.example.studentsystem_back.dto.PageDTO;
import org.example.studentsystem_back.service.MoneyService;
import org.example.studentsystem_back.mapper.MoneyMapper;
import org.example.studentsystem_back.vo.moneyMangeVo.MoneyIdVo;
import org.example.studentsystem_back.vo.moneyMangeVo.MoneyVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 马鑫
* @description 针对表【money】的数据库操作Service实现
* @createDate 2024-07-15 08:27:54
*/
@Service
public class MoneyServiceImpl extends ServiceImpl<MoneyMapper, Money>
    implements MoneyService{
    @Resource
    private MoneyMapper moneyMapper;

    @Override
    public CommonResponse<MoneyVo> findAll(PageDTO pageDTO,int num,String name){
        Page<Money> page=new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        QueryWrapper<Money> queryWrapper=new QueryWrapper<>();
        if(num>0){
            queryWrapper.eq("dorm_num",num);
        }
        queryWrapper.like("area",name);
        List<Money> list=moneyMapper.selectPage(page,queryWrapper).getRecords();
        MoneyVo moneyVo=new MoneyVo();
        List<MoneyIdVo> moneyIdVoList=new ArrayList<>();
        int i=pageDTO.getPage()*10-9;
        for(Money money:list){
            MoneyIdVo moneyIdVo=new MoneyIdVo();
            moneyIdVo.setMoneyId(money);
            moneyIdVo.setSuiId(i);
            moneyIdVoList.add(moneyIdVo);
            i++;
        }
        moneyVo.setMoneyIdVoList(moneyIdVoList);
        int total= Math.toIntExact(moneyMapper.selectCount(queryWrapper));
        moneyVo.setTotal(total);
        return CommonResponse.createForSuccess("查询成功",moneyVo);
    }

    @Override
    public CommonResponse<Money> add(Money money) {
        moneyMapper.insert(money);
        return CommonResponse.createForSuccess("增加成功",money);
    }

    @Override
    public CommonResponse<Money> update(Money money) {
        moneyMapper.updateById(money);
        return CommonResponse.createForSuccess("更新成功",money);
    }

    @Override
    public CommonResponse<Money> delete(Money money) {
        moneyMapper.deleteById(money);
        return CommonResponse.createForSuccess("删除成功",money);
    }
}




